package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryBrandRelationDao;
import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }


    // 确定品牌的id 和 品牌的名字
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        // 获取品牌id
        Long brandId = categoryBrandRelation.getBrandId();
        // 获取分类id
        Long catelogId = categoryBrandRelation.getCatelogId();

        LambdaQueryWrapper<CategoryBrandRelationEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CategoryBrandRelationEntity::getBrandId,brandId).eq(CategoryBrandRelationEntity::getCatelogId,catelogId);
        int count = count(lambdaQueryWrapper);

        // 表示已经添加过了 就不用再添加了
        if(count > 0) {
            return;
        }

        // 没有重复添加过在添加
        // 查询品牌的分类名 和 品牌名
        BrandEntity brand = brandService.getById(brandId);
        String brandName = brand.getName();
        CategoryEntity category = categoryService.getById(catelogId);
        String categoryName = category.getName();
        categoryBrandRelation.setBrandName(brandName);
        categoryBrandRelation.setCatelogName(categoryName);
        save(categoryBrandRelation);
    }

    // 更新了品牌名的话 同步更新其他关联表中的数据
    // 开启事务
    @Transactional
    @Override
    public void updateBrand(Long brandId, String name) {

        LambdaUpdateWrapper<CategoryBrandRelationEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(CategoryBrandRelationEntity::getBrandId,brandId);
        lambdaUpdateWrapper.set(CategoryBrandRelationEntity::getBrandName,name);

        // 就进行了修改了
        update(lambdaUpdateWrapper);
    }

    // 修改了分类名字后 修改我们关联的表
    @Override
    public void updateCategory(Long catId, String name) {
        LambdaUpdateWrapper<CategoryBrandRelationEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(CategoryBrandRelationEntity::getCatelogId, catId);
        lambdaUpdateWrapper.set(CategoryBrandRelationEntity::getCatelogName, name);
        update(lambdaUpdateWrapper);
    }

    /**
     * 获取catId下面的所有品牌
     * @param catId
     * @return
     */
    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        LambdaUpdateWrapper<CategoryBrandRelationEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(CategoryBrandRelationEntity::getCatelogId, catId);
        List<CategoryBrandRelationEntity> brandRelationEntities = list(lambdaUpdateWrapper);
        // 关联表里面就有我们的品牌的名字 和 brandId
        // 我们可以收集品牌的详细信息
        List<BrandEntity> brands = brandRelationEntities.stream().map((item) -> {
            Long brandId = item.getBrandId();
            BrandEntity brandEntity = brandService.getById(brandId);
            return brandEntity;
        }).collect(Collectors.toList());
        // 返回全部的品牌
        return brands;
    }

}